// pages/yuding/dizhi/add/add.js
let app = getApp();
import { judgePhone } from "../../../../utils/judgePhobe.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {

    loufangs: [{
        name: 'dianti',
        value: '电梯房'
      },
      {
        name: 'louti',
        value: '楼梯房'
      },
    ],
    loupans: [{
        name: 'xiaoqu',
        value: '小区'
      },
      {
        name: 'xiezilou',
        value: '写字楼'
      },
      {
        name: 'mingzhai',
        value: '民宅'


        
      },
    ],
    userAndAddress: {
      address: "",
      buildingNature: "",
      contactName: "",
      contactPhone: "",
      propertyNature: "",
      userId: ""
    },
    latitude: "",   //维度
    longitude: "",  //经度
    userInfo:{}
  },
  Submit: function(e) {

    var that = this;
    if (judgePhone(e.detail.value.contactPhone)) {
      that.data.userAndAddress.buildingNature = e.detail.value.buildingNature;
      that.data.userAndAddress.address = e.detail.value.address;
      that.data.userAndAddress.propertyNature = e.detail.value.propertyNature;
      that.data.userAndAddress.contactName = e.detail.value.contactName;
      that.data.userAndAddress.contactPhone = e.detail.value.contactPhone;
      wx.request({
        url: app.globalData.baseUrl + '/user/address',
        data: {
          userId: that.data.userInfo.userId,
          buildingNature: that.data.userAndAddress.buildingNature,
          address: that.data.userAndAddress.address,
          propertyNature: that.data.userAndAddress.propertyNature,
          contactName: that.data.userAndAddress.contactName,
          contactPhone: that.data.userAndAddress.contactPhone,
          latitude: that.data.latitude,   //维度
          longitude: that.data.longitude,  //经度
        },
        method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        header: { // 设置请求的 header
          'content-type': 'application/json',
          "cookie": "JSEEIONID=" + that.data.userInfo.sessionId
        },
        success: function (res) {
          console.log("submitRes", res);
          if (res.statusCode === 201) {
            wx.showToast({
              title: '添加成功！',
              icon: "success",
              duration: 1500,
              success(res) {
                var pages = getCurrentPages();
                var beforePage = pages[pages.length - 2];
                beforePage.onLoad();
                wx.navigateBack({
                  delta: 1,
                })
              }
            });
          } else {
            wx.showToast({
              title: '网络异常！',
              icon: "loading",
              duration: 1500
            })
          }
        },
        fail: function (res) {
          console.log('错误' + ':' + res)
        }
      })
    } else {
      return;
    }
    
  },

  chooseLocation() {
    let that = this;
    wx.chooseLocation({
      success: function(res) {
        that.setData({
          address: res.address+res.name,
          latitude: res.latitude,
          longitude: res.longitude
        });
        console.log(res);
      },
      fail: function(err) {
        wx.showToast({
          title: '微信服务异常！',
          icon: "loading",
          duration: 2000
        });
        //让用户开启授权
        wx.authorize({
          scope: 'scope.userLocation',
          success() {
            return;
          },
          fail() {
            
          }
        });
        console.log("chooseLocationErr",err);
      },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      userInfo: wx.getStorageSync("userInfo")
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})